<?php
/**
 * 公告信息
 * User: Tommy
 * Date: 2018/11/13
 * Time: 14:44
 */

namespace App\Controllers;


use App\Services\NoticeService;
use App\Services\SearchTimeLimitService;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class NoticeController extends BaseController
{
//    public $allows = ['getAllHall', 'addInfo', 'getNoticeInfo'];

    CONST GAME = 1;
    CONST FINANCE = 2;
    CONST PLATFORM = 3;

    /**
     * 获取厅主名称
     * @return array|bool
     */
    public function getAllHall()
    {
        return NoticeService::getHallName();
    }

    /**
     * 添加厅主公告
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\CustomException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function addInfo(Request $request)
    {
        $this->validate($request, [
            'hall_id' => 'required',
            'type'    => 'required',
            'title'   => 'required|max:255',
            'content' => 'required',
        ]);
        $value = $request->input();
        return NoticeService::addinfo($value);
    }

    /**
     * 获取公告
     * @param Request $request
     * @return array|mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function getNoticeInfo(Request $request)
    {

        $this->validate($request, [
            'id'   => 'integer',
            'page' => 'integer',
            'page_size' => 'integer',
        ]);
        $value = $request->input();
        return NoticeService::noticeInfo($value);
    }

    /**
     * 撤回
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function revert(Request $request)
    {
        $this->validate($request, [
            'notice_id' => 'required',
            'status'    => 'required',
        ]);
        $id     = $request->input('notice_id');
        $status = $request->input('status');
        return NoticeService::revert($id, $status);
    }

    /**
     *搜索框模糊搜索
     */
    public function likeQuery(Request $request)
    {
        return NoticeService::likeQuery();
    }

    /**
     * 查询公告接口
     * @param Request $request
     * @return array
     * @throws \Illuminate\Validation\ValidationException
     */
    public function query(Request $request)
    {
        $this->validate($request, [
            'user_id'    => 'integer',
            'hall_id'    => 'integer',
            'type'       => [Rule::in(self::GAME, self::FINANCE, self::PLATFORM)],
            'start_time' => 'string',
            'end_time'   => 'string',
        ]);
        $value = $request->input();
        //搜索时间限制
        if(!empty($value['start_time']) && !empty($value['end_time'])){
            SearchTimeLimitService::timeLimit($value['start_time'],$value['end_time'],SearchTimeLimitService::SIX_MONTH);
        }
        return NoticeService::query($value);
    }

    /**
     * 统计公告查看状态
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function sumStatus(Request $request)
    {
        $this->validate($request, [
            'notice_id' => 'required',
        ]);
        $value = $request->input('notice_id');
        return NoticeService::sumStatus($value);
    }
}